<?php

class Cms_Model_Container extends WSB_Db_Row
{

	/**
	 * @var Cms_Model_Xtensions
	 */
    private static $_modules;

	/**
	 * @todo: Get Cms_Model_Page object
	 *
	 */
	public function getPage() {}

	/**
	 * Gib das fuer das aktuelle Container-Objekt
	 *
	 * @return Cms_Model_Xtension
	 */
	public function getModule()
	{
        $module = null;
        if(!empty($this->instance_id)) {
            $module = $this->_getModule($this->instance_id);
        }
        return $module;
	}

	/**
	 * Cachefunktion: Die Module sollen nur einmal ausgelesen werden
	 *
	 * @param integer $instance_id
	 * @return Cms_Model_Xtension
	 */
	private function _getModule($instance_id)
	{
        if(is_null(self::$_modules)) {
        	$table = new Cms_Model_DbTable_Xtension();
        	$where = $table->getAdapter()->quoteInto('deleted=?', 0);
            self::$_modules = $table->fetchAll($where);
        }
        if(self::$_modules instanceof Cms_Model_Xtensions) {
        	return self::$_modules->getRow($instance_id);
        }
        return null;
	}

	/**
	 * @todo: Get Cms_Model_Language object
	 *
	 */
	public function getLanguage() {}

	/**
	 * @todo: Get Cms_Model_Placeholder object
	 *
	 */
	public function getPlaceholder() {}

	/**
	 * Get WSB_Xtension_Base object
	 * Mit Container und Module geladen.
	 *
	 * @return WSB_Xtension_Base
	 */
	public function getXtInstance()
	{
		try {
			$module = $this->getModule();
			if(is_null($module)) {
				return null;
			}
	    	$controllerClassname = $this->controller;
			$filename = str_replace(ucfirst($module->class).'_', '', $controllerClassname).'.php';
			$dir = WSB_XTENSION_PATH . sprintf('%s/controllers/', strtolower($module->class));
			Zend_Loader::loadFile($filename, $dir, true);
			$XT = new $controllerClassname();
	    	$XT->setModule($module);
	    	$XT->setContainer($this);
	    	return $XT;
		} catch (Zend_Exception $e) {
			// Eine Exception zu werfen, ist in diesem Kontext nicht angebracht.
			// throw new Exception(sprintf('File %s was not found', $controllerClassname));
			// echo sprintf('File %s was not found<br />', $controllerClassname);
		}
		return null;
	}

	/**
	 * Website Benutzer-Zugang zum Inhaltselement ueberpruefen
	 *
	 * @param Member_Model_DbTable_Feuser_Row $feUser
	 * @return boolean
	 */
	public function hasAccess(Member_Model_DbTable_Feuser_Row $feUser = null)
	{

		// Es sind keine FegroupIDs gespeichert:
		// -> freier Zugang zum Container (=Inhaltselement)
		if(empty($this->fegroups)) {
			return true;
		}

		// Array mit IDs
		$feGroupIds = WSB_Div::csvToArray($this->fegroups, ',');

		// Bei jedem Login verstecken
		if(in_array(-2, $feGroupIds)) {
			if($feUser instanceof Member_Model_DbTable_Feuser_Row) {
				return false;
			} else {
				return true;
			}
		}
		// Bei jedem Login anzeigen
		if(in_array(-1, $feGroupIds)) {
			if($feUser instanceof Member_Model_DbTable_Feuser_Row) {
				return true;
			} else {
				return false;
			}
		}

		// Damit sicher gestellt ist, dass nur einmal aus der DB gelesen werden muss
		static $feGroups = null;
		if($feUser instanceof Member_Model_DbTable_Feuser_Row) {
			if(is_null($feGroups)) {
				$feUser->setTable(new Member_Model_DbTable_Feuser());
				$feGroups = $feUser->findManyToManyRowset('Member_Model_DbTable_Fegroup', 'Member_Model_DbTable_Femember');
				$feGroups->unsetDeletedRows();
				$feGroups->unsetHiddenRows();
			}
			foreach($feGroups AS $feGroup) {
				if(in_array($feGroup->id, $feGroupIds)) {
					return true;
				}
			}
		}

		// Gibt per Default "false" zurueck
		return false;

	}

	/**
	 * @return boolean
	 */
	public function isRecursive()
	{
		return !empty($this->recursive);
	}

}